package com.gj.fazhi.mapper;

import com.gj.fazhi.model.*;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * Created by Tangyunhen on 2021-06-24.
 */
@Mapper
public interface FaZhiMapper {
    List<FaZhiVo> queryData(FaZhiSelectVo faZhiSelectVo);

    int queryCount(FaZhiSelectVo faZhiSelectVo);

    @Select("SELECT * FROM spart s " +
            "LEFT JOIN plant_spart ps ON s.code=ps.spartCode " +
            "LEFT JOIN factory f ON ps.plantCode=f.legalPlantCode " +
            "WHERE f.legalPersonCode =#{legalPersonCode}" +
            "and s.code like CONCAT(#{spartCode}),'%'")
    List<SpartVo> queryByCodeLike(String legalPersonCode, String spartCode);

    @Select("select * from spart where code = #{spartCode}")
    List<SpartVo> querySpartByCode(String spartCode);

    @Select("select count(id) from lagelperson where legalPersonCode=#{legalPersonCode}")
    int queryLegalPersonByCode(@NotBlank(message = "法人不能为空") String legalPersonCode);

    @Select("select * from fazhi where legalPersonCode =#{legalPersonCode} and spartCode=#{spartCode}")
    int queryFaZhiByLegalPersonCodeAndSpartCode(@NotBlank(message = "法人不能为空") String legalPersonCode, @NotBlank(message = "备件编码不能为空") String spartCode);

    @Insert("insert into spart (spartCode,fazhi,beizhu) value (#{spartCode},#{fazhi},#{beizhu})")
    void insert(FaZhiVo faZhiVo);

    @Select("select * from fazhi where id = #{id}")
    FaZhiVo updateByID(@NotNull(message = "id不能为空") Integer id);

    @Update("update fazhi set beizhu=#{beizhu},fazhi=#{fazhi} where id =#{id}")
    void update(FaZhiVo faZhiVo);
}
